<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;

/**
 * 用户每日留存管理
 *
 * @icon fa fa-circle-o
 */
class UserKeepDay extends Backend
{
    
    /**
     * UserKeepDay模型对象
     * @var \app\admin\model\UserKeepDay
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\UserKeepDay;
        $this->view->assign("typeList", $this->model->getTypeList());
    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    /**
     * 查看
     */
    public function index()
    {

        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法

        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->with(['app'])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            foreach ($list->items() as &$item) {
                if (!empty($item->scale2)) {
                    $item->scale2 = ($item->scale2 * 100) . '%';
                }
                if (!empty($item->scale3)) {
                    $item->scale3 = ($item->scale3 * 100) . '%';
                }
                if (!empty($item->scale4)) {
                    $item->scale4 = ($item->scale4 * 100) . '%';
                }
                if (!empty($item->scale5)) {
                    $item->scale5 = ($item->scale5 * 100) . '%';
                }
                if (!empty($item->scale6)) {
                    $item->scale6 = ($item->scale6 * 100) . '%';
                }
                if (!empty($item->scale7)) {
                    $item->scale7 = ($item->scale7 * 100) . '%';
                }
                if (!empty($item->scale15)) {
                    $item->scale15 = ($item->scale15 * 100) . '%';
                }
                if (!empty($item->scale30)) {
                    $item->scale30 = ($item->scale30 * 100) . '%';
                }
                if (!empty($item->scale60)) {
                    $item->scale60 = ($item->scale60 * 100) . '%';
                }
                if (!empty($item->scale90)) {
                    $item->scale90 = ($item->scale90 * 100) . '%';
                }

            }
            $result = array("total" => $list->total(),
                "rows" => $list->items()
            );

            return json($result);
        }
        return $this->view->fetch();
    }

    public function echarts($startDay = null,  $field = null, $endDay=null,$type = null)
    {
        $startDay = empty($startDay) ? date('Y-m-d', time()) : $startDay;
        $endDay = empty($endDay) ? date('Y-m-d',strtotime('next month')) : $endDay;
        $type = empty($type) ? 0 : $type;
        if ($this->request->isAjax()) {
            $field = empty($field ) ? 'day2' : $field;


            $option['legend'] = Db::name('app')->column('name');
            $option['xAxis'] = $this->model->Distinct(true)
                ->where('day_register','between time',[$startDay,$endDay])
                ->where('type',$type)
                ->column('day_register');
            $count_date = count($option['xAxis']);
            //dump($option);
            $app_list = Db::name('app')->where('status',0)->field('app_id as id,name')->select();
            foreach ($app_list as $key => $val) {
                $option['series'][$key]['type'] = 'line';
                $option['series'][$key]['name'] = $val['name'];

                $option['series'][$key]['stack'] = '百分比';
                //$data = $this->model->where('app_id',$val['id'])
                $data = $this->model->where('app_id',$val['id'])
                    ->where('day_register','between time',[$startDay,$endDay])
                    ->where('type',$type)
                    ->field("num, $field")
                    ->order('day_register')
                    ->select();
                $option['series'][$key]['data'] = [];
                foreach ($data as $k => $v) {
                    if (!empty( $v)) {
                        //$option['series']['data'][] = numbe ($val/$key)*100;
                        $option['series'][$key]['data'][] = $v[$field] * 100;// number_format($v[$field]/$v['num'],4)*100;
                    } else {
                        $option['series'][$key]['data'][] = 0;
                    }

                }
               // dump($option['series'][$key]['data']);
                $count_series_date  = count($option['series'][$key]['data']);
                //判断某个app是否有相对应的日期天数总结，没有则填充0
                $count = $count_date - $count_series_date;
                if ( $count > 0) {
                    $option['series'][$key]['data'] = array_pad($option['series'][$key]['data'], -$count_date, 0);
                }
            }
            return json($option);
        }

        $keepList = [
            'scale2' => '次日',
            'scale3' => '3日',
            'scale4' => '4日',
            'scale5' => '5日',
            'scale6' => '6日',
            'scale7' => '7日',
            'scale15' => '15日',
            'scale30' => '30日',
            'scale60' => '60日',
            'scale90' => '90日'
        ];
        $this->view->assign("startDay", $startDay);
        $this->view->assign("endDay", $endDay);
        $this->view->assign("keepList", $keepList);
        return $this->view->fetch();
    }
}
